package com.laikan.library.utils;

import android.text.TextUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Preferences {
	private static final String PREF_KEY_HAS_CACHE_THUMBNAIL = "key_has_cache_thumbnail";

	public static void setHasCacheThumbnail(boolean cached){

		SPUtil.putBoolean(PREF_KEY_HAS_CACHE_THUMBNAIL, cached);
	}

	public static boolean hasCacheThumbnail(){

		return SPUtil.putBoolean(PREF_KEY_HAS_CACHE_THUMBNAIL, false);
	}

	private static final String PREF_KEY_PHONE_NUMBER = "key_phone_number";

	public static void setPhoneNumber(String phoneNumber){

		SPUtil.putString(PREF_KEY_PHONE_NUMBER, phoneNumber);
	}

	public static String getPhoneNumber(){

		return SPUtil.getString(PREF_KEY_PHONE_NUMBER, null);
	}

	private static final String PREF_KEY_PASSWORK = "key_password";
	private static final String PREF_KEY_HASUPLOAD = "key_has_upload_uid_info";

	public static void setPassword(String password){

		SPUtil.putString(PREF_KEY_PASSWORK, password);
	}

	public static String getPassword(){

		return SPUtil.getString(PREF_KEY_PASSWORK, null);
	}

	public static void setUidInfo(boolean has_upload_uid_info){

		SPUtil.putBoolean(PREF_KEY_HASUPLOAD, has_upload_uid_info);
	}

	public static boolean getUidInfo(){

		return SPUtil.putBoolean(PREF_KEY_HASUPLOAD, false);
	}

	private static final String PREF_KEY_LOGIN_TOKEN = "key_login_token";

	public static void setLoginToken(String token){

		SPUtil.putString(PREF_KEY_LOGIN_TOKEN, token);
	}

	public static String getLoginToken(){

		return SPUtil.getString(PREF_KEY_LOGIN_TOKEN, null);
	}

	private static final String PREF_KEY_USER_ID = "key_user_id";

	public static void setUserId(String userId){

		SPUtil.putString(PREF_KEY_USER_ID, userId);
	}

	public static String getUserId(){

		return SPUtil.getString(PREF_KEY_USER_ID, null);
	}

	private static final String PREF_KEY_USER_AVATAR_URL = "key_user_avatar_url";

	public static void setUserAvatarUrl(String url){

		SPUtil.putString(PREF_KEY_USER_AVATAR_URL, url);
	}

	public static String getUserAvatarUrl(){

		return SPUtil.getString(PREF_KEY_USER_AVATAR_URL, null);
	}

	private static final String PREF_KEY_FILTER_PRICE = "key_filter_price";


	private static final String PREF_KEY_FILTER_KM = "key_filter_km";

	private static final String PREF_KEY_FILTER_BRAND = "key_filter_brand";

	public static void setCarFilterBrand(String brand){

		SPUtil.putString(PREF_KEY_FILTER_BRAND, brand);
	}

	public static String getCarFilterBrand(){

		return SPUtil.getString(PREF_KEY_FILTER_BRAND, null);
	}

	private static final String PREF_KEY_FILTER_PRODUCTOR = "key_filter_productor";

	public static void setCarFilterProducator(String brand){

		SPUtil.putString(PREF_KEY_FILTER_PRODUCTOR, brand);
	}

	public static String getCarFilterProducator(){

		return SPUtil.getString(PREF_KEY_FILTER_PRODUCTOR, null);
	}

	private static final String PREF_KEY_FILTER_MODEL = "key_filter_model";

	public static void setCarFilterModel(String model){

		SPUtil.putString(PREF_KEY_FILTER_MODEL, model);
	}

	public static String getCarFilterModel(){

		return SPUtil.getString(PREF_KEY_FILTER_MODEL, null);
	}

	private static final String PREF_KEY_FILTER_LOCATION = "key_filter_location";

	public static void setCarFilterLocation(String location){

		SPUtil.putString(PREF_KEY_FILTER_LOCATION, location);
	}

	public static String getCarFilterLocation(){

		return SPUtil.getString(PREF_KEY_FILTER_LOCATION, null);
	}

	private static final String PREF_KEY_FILTER_CAR_TYPE = "key_filter_car_type";

	private static final String PREF_KEY_FILTER_LOCATION_PRVINCE_ID = "key_filter_location_prvince_id";

	public static void setCarFilterLocationProvinceId(String provinceId){

		SPUtil.putString(PREF_KEY_FILTER_LOCATION_PRVINCE_ID, provinceId);
	}

	public static String getCarFilterLocationProvinceId(){

		return SPUtil.getString(PREF_KEY_FILTER_LOCATION_PRVINCE_ID, null);
	}

	private static final String PREF_KEY_FILTER_LOCATION_CITY_ID = "key_filter_location_city_id";

	public static void setCarFilterLocationCityId(String cityId){

		SPUtil.putString(PREF_KEY_FILTER_LOCATION_CITY_ID, cityId);
	}

	public static String getCarFilterLocationCityId(){

		return SPUtil.getString(PREF_KEY_FILTER_LOCATION_CITY_ID, null);
	}

	private static final String PREF_KEY_AUTO_LOCATION_PRVINCE_ID = "key_auto_location_prvince_id";

	public static void setAutoLocationProvinceId(String provinceId){

		SPUtil.putString(PREF_KEY_AUTO_LOCATION_PRVINCE_ID, provinceId);
	}

	public static String getAutoLocationProvinceId(){

		return SPUtil.getString(PREF_KEY_AUTO_LOCATION_PRVINCE_ID, null);
	}

	private static final String PREF_KEY_AUTO_LOCATION_CITY_ID = "key_auto_location_city_id";

	public static void setAutoLocationCityId(String cityId){

		SPUtil.putString(PREF_KEY_AUTO_LOCATION_CITY_ID, cityId);
	}

	public static String getAutoLocationCityId(){

		return SPUtil.getString(PREF_KEY_AUTO_LOCATION_CITY_ID, null);
	}

	private static final String PREF_KEY_SUBSCRIPTE_RULES = "KEY_SUBSCRIPTE_RULES";

	public static List<String> getSubsripteRules(){

		List<String> list = new ArrayList<String>();
		String prefValue = SPUtil.getString(PREF_KEY_SUBSCRIPTE_RULES, null);
		if (!TextUtils.isEmpty(prefValue)){
			String[] rules = prefValue.split("|");
			for (String rule : rules){
				list.add(rule);
			}
		}
		return list;
	}

	public static void setSubscripteRules(List<String> rules){

		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < rules.size(); i++){
			sb.append(rules.get(i));
			if (i != rules.size() - 1){
				sb.append("|");
			}
		}
		SPUtil.putString(PREF_KEY_SUBSCRIPTE_RULES, sb.toString());
	}

	public static boolean isNewSubscripteRule(Map<String, String> params){

		StringBuilder sb = new StringBuilder();
		Set<String> keys = params.keySet();
		for (String key : keys){
			sb.append(key);
			sb.append(":");
			sb.append(params.get(key));
			sb.append("-");
		}
		String newRule = sb.toString();
		newRule = newRule.substring(0, newRule.length() - 1);
		List<String> rules = Preferences.getSubsripteRules();
		for (String rule : rules){
			if (TextUtils.equals(rule, newRule)){
				return true;
			}
		}
		return false;
	}

	public static void editSubscripteRules(Map<String, String> params, boolean isDelete){

		StringBuilder sb = new StringBuilder();
		Set<String> keys = params.keySet();
		for (String key : keys){
			sb.append(key);
			sb.append(":");
			sb.append(params.get(key));
			sb.append("-");
		}
		String newRule = sb.toString();
		newRule = newRule.substring(0, newRule.length() - 1);
		List<String> rules = Preferences.getSubsripteRules();
		if (isDelete){
			rules.remove(newRule);
		} else{
			rules.add(newRule);
		}
	}

	private static final String PREF_KEY_SUBSCRIPTE_RULES_COUNT = "key_subscripte_rules_count";

	private static final String PREF_KEY_SETTINGS_PUSH_SOUND_ENABLED = "key_settings_push_sound_enabled";

	public static void setSettingsPushSoundEnabled(boolean enabled){

		SPUtil.putBoolean(PREF_KEY_SETTINGS_PUSH_SOUND_ENABLED, enabled);
	}

	public static boolean getSettingsPushSoundEnabled(){

		return SPUtil.putBoolean(PREF_KEY_SETTINGS_PUSH_SOUND_ENABLED, true);
	}

	private static final String PREF_KEY_SETTINGS_PUSH_VIBRATE_ENABLED = "key_settings_push_vibrate_enabled";

	public static void setSettingsPushVibrateEnabled(boolean enabled){

		SPUtil.putBoolean(PREF_KEY_SETTINGS_PUSH_VIBRATE_ENABLED, enabled);
	}

	public static boolean getSettingsPushVibrateEnabled(){

		return SPUtil.putBoolean(PREF_KEY_SETTINGS_PUSH_VIBRATE_ENABLED, true);
	}

	private static final String PREF_KEY_SETTINGS_PUSH_ENABLED = "key_settings_push_enabled";

	public static void setSettingsPushEnabled(boolean enabled){

		SPUtil.putBoolean(PREF_KEY_SETTINGS_PUSH_ENABLED, enabled);
	}

	public static boolean getSettingsPushEnabled(){

		return SPUtil.putBoolean(PREF_KEY_SETTINGS_PUSH_ENABLED, true);
	}

	private static final String PREF_KEY_HAS_SHOW_MAIN_GUIDE = "key_has_show_main_guide";

	public static void setHasShowMainGuide(boolean shown){

		SPUtil.putBoolean(PREF_KEY_HAS_SHOW_MAIN_GUIDE, shown);
	}

	public static boolean hasShowMainGuide(){

		return SPUtil.putBoolean(PREF_KEY_HAS_SHOW_MAIN_GUIDE, false);
	}

	private static final String PREF_KEY_HAS_SHOW_SUBSCRIBE_GUIDE = "key_has_show_subscribe_guide";

	public static void setHasShowSubscribeGuide(boolean shown){

		SPUtil.putBoolean(PREF_KEY_HAS_SHOW_SUBSCRIBE_GUIDE, shown);
	}

	public static boolean hasShowSubscribeGuide(){

		return SPUtil.putBoolean(PREF_KEY_HAS_SHOW_SUBSCRIBE_GUIDE, false);
	}

	private static final String PREF_KEY_NEW_FRIEND_TRNEDS_COUNT = "key_new_friend_trends_count";


	private static final String PREF_KEY_NEW_SUBSCRIBE_COUNT = "key_new_subscribe_count";


	private static final String PREF_KEY_IS_OPEN_DEBUG = "key_is_open_debug_mode";

	public static void setIsDebugMode(boolean isDebugMode){

		SPUtil.putBoolean(PREF_KEY_IS_OPEN_DEBUG, isDebugMode);
	}

	public static boolean getIsDebugMode(){

		return SPUtil.putBoolean(PREF_KEY_IS_OPEN_DEBUG, false);
	}

	private static final String PREF_KEY_IS_REQUEST_TEST_SERVER = "key_is_request_test_server";

	public static void setIsRequestTestServer(boolean isDebugMode){

		SPUtil.putBoolean(PREF_KEY_IS_REQUEST_TEST_SERVER, isDebugMode);
	}

	public static boolean getIsRequestTestServer(){

		return SPUtil.putBoolean(PREF_KEY_IS_REQUEST_TEST_SERVER, false);
	}

	public static final String PREF_KEY_CHEDD_ACTIVITY_ID = "chedd_activity_id";// 车多多首页弹窗推广活动的ID

	public static void setCheddActivityId(String id){

		SPUtil.putString(PREF_KEY_CHEDD_ACTIVITY_ID, id);
	}

	public static String getCheddActivityId(){

		return SPUtil.getString(PREF_KEY_CHEDD_ACTIVITY_ID, "");
	}

	private static final String PREF_KEY_DIFF_ACTION = "key_diff_action";

	public static void setDiffAction(String actionMD5){

		SPUtil.putString(PREF_KEY_DIFF_ACTION, actionMD5);
	}

	public static String getDiffAction(){

		return SPUtil.getString(PREF_KEY_DIFF_ACTION, "");
	}

	private static final String HAS_UPLOAD_IMEI = "key_has_upload_imei";

	public static void setHasUploadIMEI(boolean upload){

		SPUtil.putBoolean(HAS_UPLOAD_IMEI, upload);
	}

	public static boolean getHasUploadIMEI(){

		return SPUtil.putBoolean(HAS_UPLOAD_IMEI, false);
	}

}
